package com.clearliang.nativeapp;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;

import com.clearliang.nativeapp.plugins.BasicPlugin;
import com.clearliang.nativeapp.plugins.EventPlugin;
import com.clearliang.nativeapp.plugins.MethodPlugin;
import com.clearliang.nativeapp.utils.TranslateUtil;
import com.clearliang.nativeapp.view.GlobalParam;

import java.util.Map;

import io.flutter.app.FlutterFragmentActivity;
import io.flutter.facade.Flutter;
import io.flutter.view.FlutterView;

/**
 * Created by ClearLiang on 2019/6/4
 * Description：
 * Function :
 */
public class MainFlutter extends FlutterFragmentActivity {

    private String routeStr = "";
    private FlutterView flutterView;

    public static void startActivity(Context context, String route) {
        Intent intent = new Intent(context, MainFlutter.class);
        intent.putExtra("route", route);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, String route, Map<String, String> params) {
        Intent intent = new Intent(context, MainFlutter.class);
        intent.putExtra("route", route + "?" + TranslateUtil.getMapToString(params));
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flutter);

        // 根据route选择Widget
        initFlutterView();
        // 注册插件
        registerCustomPlugin();

    }

    private void initFlutterView() {
        Intent intent = getIntent();
        routeStr = intent.getStringExtra("route");
        if (null == routeStr || "".equals(routeStr)) {
            routeStr = GlobalParam.RouteStart;
        }
        // flutter 入口
        flutterView = Flutter.createView(
                this,
                getLifecycle(),
                routeStr//这里根据路由不用，跳转不同 Flutter Weight
        );

        final FrameLayout layout = findViewById(R.id.flutter_container);
        layout.addView(flutterView);

        FlutterView.FirstFrameListener[] listeners = new FlutterView.FirstFrameListener[1];
        listeners[0] = new FlutterView.FirstFrameListener() {
            @Override
            public void onFirstFrame() {
                findViewById(R.id.flutter_progress).setVisibility(View.INVISIBLE);
                layout.setVisibility(View.VISIBLE);
            }
        };
        flutterView.addFirstFrameListener(listeners[0]);

    }

    private void registerCustomPlugin() {
        BasicPlugin.registerWith(flutterView);
        MethodPlugin.registerWith(flutterView);
        EventPlugin.registerWith(flutterView);
    }

    @Override
    public void onBackPressed() {
        if (this.flutterView != null) {
            this.flutterView.popRoute();
        } else {
            super.onBackPressed();
        }
    }
}
